#!/bin/bash

# DIRECCION IP SERVIDOR WEB
IP_WEB="192.168.15.1"
BROADCAST_WEB="192.168.15.255"
MASCARA_WEB="255.255.255.0"

# DIRECCIONES IP SERVIDOR TELNET
IP_TELNET1="10.31.101.130"
BROADCAST_TELNET1="10.31.101.255"
MASCARA_TELNET1="255.255.255.0"
IP_TELNET2="10.4.203.129"
BROADCAST_TELNET2="10.4.203.159"
MASCARA_TELNET2="255.255.255.0"

# DIRECCION IP SERVIDOR FTP
IP_FTP="10.6.1.1"
BROADCAST_FTP="10.6.1.255"
MASCARA_FTP="255.255.255.0"

# DIRECCIONES DE RED
atom="10.4.203.64"
benji="10.55.6.0"
callahan="192.168.15.0"
dtfrank="10.4.203.96"
eddie="10.31.101.0"
oliver="10.4.203.128"
fleming="172.17.1.64"
gamou="10.55.7.144"
hyuga="10.55.7.0"
ichiro="10.55.7.148"
johnson="10.4.203.192"
koriotto="10.4.203.0"
leblanc="10.55.7.128"
misaki="10.6.1.0"
napoleon="10.4.203.224"

# GATEWAYS WEB SERVER
G_W_atom="192.168.15.4" #VRRP R4-R5
G_W_benji="192.168.15.4" #VRRP R4-R5
G_W_callahan="192.168.15.4" #VRRP R4-R5
G_W_dtfrank="192.168.15.100" #R8
G_W_eddie="192.168.15.4" #VRRP R4-R5
G_W_oliver="192.168.15.100" #R8
G_W_fleming="192.168.15.4" #VRRP R4-R5
G_W_gamou="192.168.15.4" #VRRP R4-R5
G_W_hyuga="192.168.15.100" #R8
G_W_ichiro="192.168.15.100" #R8
G_W_johnson="192.168.15.100" #R8
G_W_koriotto="192.168.15.4" #VRRP R4-R5
G_W_leblanc="192.168.15.100" #R8
G_W_misaki="192.168.15.100" #R8
G_W_napoleon="192.168.15.100" #R8

# GATEWAYS TELNET SERVER
G_T_atom="10.4.203.129" #R10
G_T_benji="10.4.203.129" #R10
G_T_callahan="10.4.203.129" #R10
G_T_dtfrank="10.4.203.129" #R10
G_T_eddie="10.4.203.129" #R10
G_T_oliver="10.4.203.129" #R10
G_T_fleming="10.4.203.129" #R10
G_T_gamou="10.4.203.129" #R10
G_T_hyuga="10.4.203.129" #R10
G_T_ichiro="10.4.203.129" #R10
G_T_johnson="10.4.203.129" #R10
G_T_koriotto="10.4.203.129" #R10
G_T_leblanc="10.4.203.129" #R10
G_T_misaki="10.4.203.129" #R10
G_T_napoleon="10.4.203.129" #R10

# GATEWAYS FTP SERVER
G_F_atom="10.6.1.4" #VRRP R16-R17
G_F_benji="10.6.1.4" #VRRP R16-R17
G_F_callahan="10.6.1.4" #VRRP R16-R17
G_F_dtfrank="10.6.1.4" #VRRP R16-R17
G_F_eddie="10.6.1.4" #VRRP R16-R17
G_F_oliver="10.6.1.4" #VRRP R16-R17
G_F_fleming="10.6.1.4" #VRRP R16-R17
G_F_gamou="10.6.1.4" #VRRP R16-R17
G_F_hyuga="10.6.1.4" #VRRP R16-R17
G_F_ichiro="10.6.1.4" #VRRP R16-R17
G_F_johnson="10.6.1.4" #VRRP R16-R17
G_F_koriotto="10.6.1.4" #VRRP R16-R17
G_F_leblanc="10.6.1.5" #R15
G_F_misaki="10.6.1.4" #VRRP R16-R17
G_F_napoleon="10.6.1.4" #VRRP R16-R17

# MASCARAS DE RED
M_atom="255.255.255.224"
M_benji="255.255.255.0"
M_callahan="255.255.255.0"
M_dtfrank="255.255.255.224"
M_eddie="255.255.255.0"
M_oliver="255.255.255.224"
M_fleming="255.255.255.192"
M_gamou="255.255.255.252"
M_hyuga="255.255.255.128"
M_ichiro="255.255.255.252"
M_johnson="255.255.255.224"
M_koriotto="255.255.255.192"
M_leblanc="255.255.255.240"
M_misaki="255.255.255.0"
M_napoleon="255.255.255.224"

function configurarServidorWeb(){

	#CONFIGURACION INTERFACES
	ifconfig $interfaz $IP_WEB broadcast $BROADCAST_WEB netmask $MASCARA_WEB 

	#Ruteo Estatico
	route add -net $atom netmask $M_atom gw $G_W_atom dev $interfaz
	route add -net $benji netmask $M_benji gw $G_W_benji dev $interfaz
	route add -net $dtfrank netmask $M_dtfrank gw $G_W_dtfrank dev $interfaz
	route add -net $eddie netmask $M_eddie gw $G_W_eddie dev $interfaz
	route add -net $oliver netmask $M_oliver gw $G_W_oliver dev $interfaz
	route add -net $fleming netmask $M_fleming gw $G_W_fleming dev $interfaz
	route add -net $gamou netmask $M_gamou gw $G_W_gamou dev $interfaz
	route add -net $hyuga netmask $M_hyuga gw $G_W_hyuga dev $interfaz
	route add -net $ichiro netmask $M_ichiro gw $G_W_ichiro dev $interfaz
	route add -net $johnson netmask $M_johnson gw $G_W_johnson dev $interfaz
	route add -net $koriotto netmask $M_koriotto gw $G_W_koriotto dev $interfaz
	route add -net $leblanc netmask $M_leblanc gw $G_W_leblanc dev $interfaz
	route add -net $misaki netmask $M_misaki gw $G_W_misaki dev $interfaz
	route add -net $napoleon netmask $M_napoleon gw $G_W_napoleon dev $interfaz

	#Copia el contenido que publica el WebServer
	cp indexGrupo1.html /var/www/index.html
	
	#Reinicia el servicio apache.
	sudo /etc/init.d/apache2 restart

	#TODO: Llamar a la configuracion dns
	cd ../DNS/
	chmod +x *
	./dns.sh host resto
}


function configurarServidorTelnet(){
	
	#CONFIGURACION INTERFACES
	interfaz2=$interfaz":0"
	ifconfig $interfaz $IP_TELNET1 broadcast $BROADCAST_TELNET1 netmask $MASCARA_TELNET1
	ifconfig $interfaz2 $IP_TELNET2 broadcast $BROADCAST_TELNET2 netmask $MASCARA_TELNET2

	#RUTEO ESTATICO
	route add -net $atom netmask $M_atom gw $G_T_atom dev $interfaz
	route add -net $benji netmask $M_benji gw $G_T_benji dev $interfaz
	route add -net $callahan netmask $M_callahan gw $G_T_callahan dev $interfaz
	route add -net $dtfrank netmask $M_dtfrank gw $G_T_dtfrank dev $interfaz
	route add -net $oliver netmask $M_oliver gw $G_T_oliver dev $interfaz
	route add -net $fleming netmask $M_fleming gw $G_T_fleming dev $interfaz
	route add -net $gamou netmask $M_gamou gw $G_T_gamou dev $interfaz
	route add -net $hyuga netmask $M_hyuga gw $G_T_hyuga dev $interfaz
	route add -net $ichiro netmask $M_ichiro gw $G_T_ichiro dev $interfaz
	route add -net $johnson netmask $M_johnson gw $G_T_johnson dev $interfaz
	route add -net $koriotto netmask $M_koriotto gw $G_T_koriotto dev $interfaz
	route add -net $leblanc netmask $M_leblanc gw $G_T_leblanc dev $interfaz
	route add -net $misaki netmask $M_misaki gw $G_T_misaki dev $interfaz
	route add -net $napoleon netmask $M_napoleon gw $G_T_napoleon dev $interfaz

	#CONFIGURACION SERVICIO TELNET
	cp inetd.conf.telnet /etc/inetd.conf
	#/etc/init.d/xinetd restart
	service xinetd restart  

	#TODO: Llamar a la configuracion dns
	cd ../DNS/
	chmod +x *
	./dns.sh host resto
}

function configurarServidorFtp(){
	ifconfig $interfaz $IP_FTP broadcast $BROADCAST_FTP netmask $MASCARA_FTP

	route add -net $atom netmask $M_atom gw $G_F_atom dev $interfaz
	route add -net $benji netmask $M_benji gw $G_F_benji dev $interfaz
	route add -net $callahan netmask $M_callahan gw $G_F_callahan dev $interfaz
	route add -net $dtfrank netmask $M_dtfrank gw $G_F_dtfrank dev $interfaz
	route add -net $eddie netmask $M_eddie gw $G_F_eddie dev $interfaz
	route add -net $oliver netmask $M_oliver gw $G_F_oliver dev $interfaz
	route add -net $fleming netmask $M_fleming gw $G_F_fleming dev $interfaz
	route add -net $gamou netmask $M_gamou gw $G_F_gamou dev $interfaz
	route add -net $hyuga netmask $M_hyuga gw $G_F_hyuga dev $interfaz
	route add -net $ichiro netmask $M_ichiro gw $G_F_ichiro dev $interfaz
	route add -net $johnson netmask $M_johnson gw $G_F_johnson dev $interfaz
	route add -net $koriotto netmask $M_koriotto gw $G_F_koriotto dev $interfaz
	route add -net $leblanc netmask $M_leblanc gw $G_F_leblanc dev $interfaz
	route add -net $napoleon netmask $M_napoleon gw $G_F_napoleon dev $interfaz

	cp inetd.conf.ftp /etc/inetd.conf
	/etc/init.d/vsftpd start

	#TODO: Llamar configuracion DNS
	cd ../DNS/
	chmod +x *
	./dns.sh host cataratas
}

#****************************************************#
#******************SETUP GENERAL*********************#
#****************************************************#
#Reinicia interfases
./setUp_Interfases.sh
#Obtiene el nombre de la interfaz de la pc
interfaz=`ifconfig | egrep "eth" | sed 's/^\([A-Za-z0-9:]*\).*/\1/'`
#Desactiva el ruteo automatico del sistema operativo
echo "0" >> /proc/sys/net/ipv4/ip_forward
#****************************************************#

case $1 in
s_web) configurarServidorWeb;;
s_telnet) configurarServidorTelnet;;
s_ftp) configurarServidorFtp;;
*) echo "Parametros incorrectos. Uso: servidor [tipo] siendo tipo={s_web,s_telnet,s_ftp}"
esac 
